package uns.fit.gis.ui;

import java.io.ByteArrayInputStream;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.Util;
import uns.fit.gis.httpconnection.MHttpConnItem;
import uns.fit.gis.httpconnection.MHttpConnection;
import uns.fit.gis.httpconnection.MHttpConnectionHandler;
import uns.fit.gis.xml.BasicHandler;

public class LoginForm extends Form implements CommandListener, MHttpConnectionHandler {
	private MapDrawer midlet;

	private TextField username, password;
	private TextField url;
	private TextField delay;
	private TextField id;
	private Command okCommand;
	private Command backCommand;
	private StringBuffer b = new StringBuffer();
	private String info;
	private String data;

        private SAXParserFactory factory;
	private SAXParser parser;
	
	public LoginForm(String title, MapDrawer midlet) {
		super(title);
		this.midlet = midlet;

		okCommand = new Command("OK", Command.OK, 1);
		addCommand(okCommand);
		backCommand = new Command("Quay lại", Command.BACK, 2);
		addCommand(backCommand);
		/*
		username = new TextField("Username","",10,TextField.ANY);
		append(username);
		password = new TextField("Password","",10,TextField.PASSWORD);
		append(password);
		*/
		url = new TextField("URL", "", 300, TextField.ANY);
		append(url);
		delay = new TextField("Delay","",10,TextField.NUMERIC);
		append(delay);
		id = new TextField("Id","",10,TextField.NUMERIC);
		append(id);
		setCommandListener(this);
	}

	public LoginForm(String title) {
		super(title);
		// TODO Auto-generated constructor stub
	}

	public LoginForm(String arg0, Item[] arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public void commandAction(Command command, Displayable displayable) {
		if (command == okCommand) {
			/*
			if (username.getString().equals("")) {

			} else {
				new Thread() {
					public void run() {
						login(username.getString(), password.getString());
					}
				}.start();
				
			}
			*/
			Util.UPDATE_POSITION_URL = url.getString();
			Util.SERVER_DELAY_TIME = Integer.valueOf(delay.getString()).intValue();
			Util.DEVICE_ID = Integer.valueOf(id.getString()).intValue();
			midlet.showMapCanvas();
		} else if (command == backCommand) {
			Util.UPDATE_POSITION_URL = url.getString();
			Util.SERVER_DELAY_TIME = Integer.valueOf(delay.getString()).intValue();
			Util.DEVICE_ID = Integer.valueOf(id.getString()).intValue();
			midlet.showMapCanvas();
		}
	}

	private void login(String username, String password) {
		
		String url = "http://localhost:1234/WebSite1/Service.asmx/HelloWorld";
		
		
                    String data = "username=" + username + "&password=" + password;
                    MHttpConnItem item = new MHttpConnItem(0, this, url, false, true, data.getBytes());
                    MHttpConnection conn = new MHttpConnection();
                    conn.addItem(item);
                    conn.start();

			
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

    public int HttpLoadCompleted(byte[] data, int errorCode, MHttpConnItem item) {
        if (data == null)
            System.out.println("NULL");
        else {
            String strXML = new String(data);
            System.out.println(strXML);
            try {
                factory = SAXParserFactory.newInstance();
                parser = factory.newSAXParser();

                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                parser.parse(bais, new BasicHandler());
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }
}
